package com.example.demo2.controller;

import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@CrossOrigin(origins = "http://localhost:8080") // 前后端跨域访问
@RestController
@RequestMapping("/api/users")

public class UserController {

    // 模拟一个内存中的“数据库”
    private List<User> mockUsers = new ArrayList<>();
    private Long nextId = 1L;

    public UserController() {
        // 初始化一些测试数据
        mockUsers.add(new User(nextId++, "alice", "password123", "alice@example.com"));
        mockUsers.add(new User(nextId++, "bob", "password456", "bob@example.com"));
    }

    // 1. 用户登录 (POST /api/users/login)
    @PostMapping("/login")
    public ApiResponse login(@RequestBody LoginRequest request) {
        for (User user : mockUsers) {
            if (user.getUsername().equals(request.getUsername()) && user.getPassword().equals(request.getPassword())) {
                return new ApiResponse(true, "Login successful", user);
            }
        }
        return new ApiResponse(false, "Invalid username or password", null);
    }

    // 2. 用户注册 (POST /api/users/register)
    @PostMapping("/register")
    public ApiResponse register(@RequestBody User newUser) {
        // 检查用户名是否已存在
        for (User user : mockUsers) {
            if (user.getUsername().equals(newUser.getUsername())) {
                return new ApiResponse(false, "Username already exists", null);
            }
        }
        newUser.setId(nextId++);
        mockUsers.add(newUser);
        return new ApiResponse(true, "Registration successful", newUser);
    }

    // 3. 获取所有用户信息 (GET /api/users) - 实际项目中需要权限控制
    @GetMapping("")
    public List<User> getAllUsers() {
        return mockUsers;
    }

    // 4. 根据ID获取用户信息 (GET /api/users/{id})
    @GetMapping("/{id}")
    public ApiResponse getUserById(@PathVariable Long id) {
        for (User user : mockUsers) {
            if (Objects.equals(user.getId(), id)) {
                return new ApiResponse(true, "User found", user);
            }
        }
        return new ApiResponse(false, "User not found", null);
    }

    // 内部使用的数据模型类 (DTO)
    public static class User {
        private Long id;
        private String username;
        private String password;
        private String email;

        // 构造方法、Getter和Setter (非常重要！)
        public User() {}
        public User(Long id, String username, String password, String email) {
            this.id = id;
            this.username = username;
            this.password = password;
            this.email = email;
        }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }
    }

    public static class LoginRequest {
        private String username;
        private String password;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }

    public static class ApiResponse {
        private boolean success;
        private String message;
        private Object data;

        public boolean isSuccess() {
            return success;
        }

        public void setSuccess(boolean success) {
            this.success = success;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public Object getData() {
            return data;
        }

        public void setData(Object data) {
            this.data = data;
        }

        public ApiResponse(boolean success, String message, Object data) {
            this.success = success;
            this.message = message;
            this.data = data;
        }
    }
}
